function toggleModal(action){
	//清理之前可能存在的数据
	document.getElementById("updateNavId").value="";
	document.getElementById("inputSiteName").value="";
	document.getElementById("inputURL").value="";
	document.getElementById("imageSource").value="";
	document.getElementById("iconsCollect").innerHTML = "";
	
	var modalName = "";
	var actionBtn = "";
	var formAction = "";
	if(action == "0"){//add
		modalName = "新增站点";
		actionBtn = "添加";
//		formAction = "add.action"
	}else{//update
		modalName = "修改站点";
		actionBtn = "更新";
//		formAction = "update.action";
	}
	document.getElementById("modalName").innerHTML=modalName;
	document.getElementById("actionBtn").innerHTML=actionBtn;
	document.getElementById("actionBtn").name=action;
//	document.getElementById("addingItemForm").action=formAction;
}

function fetchIconUrls(self){
	$(self).attr("disabled","disabled");
	var url = $("#inputURL").val();
	$.ajax({
		url:"fetchUrls.action",
		dataType:"json",
		data:"url="+url,
		success:function(data){//成功后的回调函数
			console.log(data.length);
			var iconsDiv = $("#iconsCollect");//存放img的div
			iconsDiv.html("");
            for(var s in data){
            	console.log("URL>>>"+data[s]);
            	var imgItem = $("<img>").addClass("siteicon").attr("src", data[s]);//生成img标签
//                	imgItem.bind("click", function(){//绑定事件
//                		//console.log($(this).attr("src"));
//                		$(this).addClass("selectedIcon");
//                		$("#iconsCollect img").removeClass("selectedIcon");
//                	});
            	imgItem.appendTo(iconsDiv);
            }
            //调用成功后,恢复按钮
            $(self).attr("disabled",false);
		},
		error:function(){
			$(self).attr("disabled",false);
		}
	}).responseText;
}

/**
 * 点击修改后a标签绑定事件
 */
function modifyItem(event){
	event.stopPropagation();
	event.preventDefault();//阻止默认点击事件
	var div = event.delegateTarget;
	var target = event.target;
	var a = div.getElementsByTagName("a")[0];
	var siteName = $(a).find("span").text();
	var navid = a.getAttribute("navid");
	var imgSrc = a.getElementsByTagName("img")[0].src;
	var siteUrl = a.href;
	if(target.tagName.toUpperCase() == "IMG" && target.alt == "delete"){
		return;
	}
	
	document.getElementById("updateNavId").value = navid;
	document.getElementById("inputSiteName").value = siteName;
	document.getElementById("inputURL").value = siteUrl;
	document.getElementById("imageSource").value = imgSrc;
	
	document.getElementById("iconsCollect").innerHTML = "<img class='siteicon' src='"+imgSrc+"'>";
//	$.post("fetchItemDetail.action", {"navid": navid}, function(data){
//		document.getElementById("updateSiteName").value=data['siteName'];
//		document.getElementById("updateNavId").value=data['navid'];
//		document.getElementById("updateSiteURL").value=data['url'];
//		document.getElementById("updateImgsrc").value=data['imgSrc'];
//	}, "json");
	$("#addItemModal").modal();
}

function clearForm(){
	$("#inputSiteName").val("");
	$("#inputURL").val("");
	$("#imageSource").val("");
}

function preDoSearch(e){
	if(e.keyCode == 13){//回车键
		doSearch();
	}
}

function doSearch(){
	var keywords = document.getElementById("searchkw").value;
	if(keywords == null || keywords == "" || keywords.trim() == ""){
		return ;
	}
	var keywordarray = keywords.split(/\s+/);
	var querystr = keywordarray.join("+");
	var url = "https://search.disconnect.me/searchTerms/search?location=CN&query=%s&ses_nojs=Google&create=&location_option=CN&lang=&option=&from_homepage=&from_form=true&source=&showIcons=&filterIcons=&js_enabled=1&ses=Google";
	url = url.replace("%s", querystr);
	window.open(url);
}

function deleteItem(e){
	var target = $(e.target);
	var siteName = target.parent().parent().find("span").text();
	var div = target.parent().parent()[0];
	var navid = div.id;
	if(target[0].tagName.toUpperCase() == "IMG" && target[0].alt == "delete"){
		var decision = window.confirm("确定删除站点：" + siteName + "?");
		if(decision){
			$.ajax({
				url: "delete.action", 
				data: "navid="+navid, 
				complete: function(){
					div.parentNode.removeChild(div);
				}
			});
			return;
		}else{
			return;
		}
	}
}

(function(){
	console.log("%c              ٩(͡๏̯͡๏)۶"," text-shadow: 0 1px 0 #ccc,0 2px 0 #c9c9c9,0 3px 0 #bbb,0 4px 0 #b9b9b9,0 5px 0 #aaa,0 6px 1px rgba(0,0,0,.1),0 0 5px rgba(0,0,0,.1),0 1px 3px rgba(0,0,0,.3),0 3px 5px rgba(0,0,0,.2),0 5px 10px rgba(0,0,0,.25),0 10px 10px rgba(0,0,0,.2),0 20px 20px rgba(0,0,0,.15);font-size:5em")
})();

function fun(){
    alert("fun");
}